'use strict';

class HttpException extends Error {
  constructor(returnMessage = '服务器异常', returnCode = 9999, code = 200) {
    super();
    this.code = code;
    this.returnMessage = returnMessage;
    this.returnCode = returnCode;
  }
}

class ParameterException extends HttpException {
  constructor(returnMessage, returnCode) {
    super();
    this.code = 200;
    this.returnMessage = returnMessage || '参数错误';
    this.returnCode = returnCode || 2999;
  }
}

// 2999 报错
class GeneralException extends HttpException {
  constructor(returnMessage, returnCode, bean = {}, beans = []) {
    super();
    this.code = 200;
    this.returnMessage = returnMessage || '参数错误';
    this.returnCode = returnCode || 2999;
    this.bean = bean;
    this.beans = beans;
  }
}

// 拋出异常的方式处理成功的请求
class Success extends HttpException {
  constructor(returnMessage, bean = {}, beans = [], returnCode) {
    super();
    this.code = 200;
    this.returnMessage = returnMessage || '操作成功';
    this.returnCode = returnCode || '0000';
    this.bean = bean;
    this.beans = beans;
  }
}

class NotFound extends HttpException {
  constructor(returnMessage, returnCode) {
    super();
    this.code = 404;
    this.returnMessage = returnMessage || '资源未找到';
    this.returnCode = returnCode || 10000;
  }
}

class AuthFailed extends HttpException {
  constructor(returnMessage, returnCode) {
    super();
    this.code = 401;
    this.returnMessage = returnMessage || '授权失败';
    this.returnCode = returnCode || 10004;
  }
}

class Forbbiden extends HttpException {
  constructor(returnMessage, returnCode) {
    super();
    this.code = 403;
    this.returnMessage = returnMessage || '禁止访问';
    this.returnCode = returnCode || 10006;
  }
}

class Exist extends HttpException { // 已存在
  constructor(returnMessage, returnCode) {
    super();
    this.code = 201;
    this.returnMessage = returnMessage || 'ok';
    this.returnCode = returnCode || 1;
  }
}

module.exports = {
  HttpException,
  Success,
  NotFound,
  AuthFailed,
  Forbbiden,
  GeneralException,
  ParameterException,
  Exist,
};
